package com.cuotigou.controller;

import com.cuotigou.config.ConfigUtils;
import com.cuotigou.mapper.SWrongTopicMapper;
import com.cuotigou.pojo.SWrongTopic;
import com.cuotigou.util.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2018/12/28.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private SWrongTopicMapper sWrongTopicMapper;

    @RequestMapping("/index")
    @ResponseBody
    public ObjectResult image() throws IOException {
        return ObjectResult.build("1","成功",1);
    }

    @RequestMapping("/image")
    public void image(XMLBean xmlBean,HttpServletResponse response) throws IOException {
        response.sendRedirect(ConfigUtils.FILE_URL+"image/news/"+xmlBean.getTitle()+".jpg");
    }

    @RequestMapping("/html")
    public ModelAndView image(XMLBean xmlBean) {
        ModelAndView modelAndView = new ModelAndView(xmlBean.getTitle());
        return modelAndView;
    }

    /**
     * 学情分析
     * @param xmlBean
     * @return
     * @throws IOException
     */
    @RequestMapping("/html/canvas")
    public ModelAndView canvas(XMLBean xmlBean) throws IOException {
        ModelAndView modelAndView = new ModelAndView("canvas");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        modelAndView.addObject("userId",userId);
        return modelAndView;
    }

    /**
     * 错题悬赏
     * @param xmlBean
     * @return
     * @throws IOException
     */
    @RequestMapping("/html/xuanshang")
    public ModelAndView xuanshang(XMLBean xmlBean) throws IOException {
        ModelAndView modelAndView = new ModelAndView("xuanshang");
        modelAndView.addObject("id",xmlBean.getId());
        return modelAndView;
    }

    /**
     * 答案解析
     * @param xmlBean
     * @return
     * @throws IOException
     */
    @RequestMapping("/html/daanjiexi")
    public ModelAndView daanjiexi(XMLBean xmlBean) throws IOException {
        ModelAndView modelAndView = new ModelAndView("daanjiexi");
        SWrongTopic sWrongTopic = sWrongTopicMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));
        modelAndView.addObject("title",StringEscapeUtils.unescapeHtml4(sWrongTopic.getStem()));
        modelAndView.addObject("content",StringEscapeUtils.unescapeHtml4(sWrongTopic.getAnswer()));
        return modelAndView;
    }

    /**
     * 视频分享
     * @param xmlBean
     * @return
     * @throws IOException
     */
    @RequestMapping("/html/videoShare")
    public ModelAndView videoShare(XMLBean xmlBean) throws IOException {
        ModelAndView modelAndView = new ModelAndView("videoShare");
        modelAndView.addObject("id",xmlBean.getId());
        return modelAndView;
    }
}
